import { wxRequest } from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    id: '',
    supplyItem: {}, //供应商
    devTypeItem: {}, //设备类型
  },

  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getDetail()
    }
  },


  onShow: function () {
  },


  formSubmit: function (e) {
    var formdata = e.detail.value;

    formdata.supId = this.data.supplyItem.id;
    formdata.supName = this.data.supplyItem.supName;

    formdata.devTypeId = this.data.devTypeItem.id
    formdata.devTypeName = this.data.devTypeItem.typeName

    if (!formdata.devTypeId || formdata.devTypeId == '') {
      wx.showToast({
        icon: 'none',
        title: '设备类型不能为空',
      })
      return
    }

    if (!formdata.supId || formdata.supId == '') {
      wx.showToast({
        icon: 'none',
        title: '请选择供应商',
      })
      return
    }

    if (!formdata.brandName) {
      wx.showToast({
        icon: 'none',
        title: '名称不能为空',
      })
      return
    }

    this.formRequest(formdata)
  },

  //请求
  formRequest: function (formdata) {
    var url = '/gds/protected/comm/beanCrud/gds.BrandVO/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/gds/protected/comm/beanCrud/gds.BrandVO/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }

    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/brand/list',
        })
      }, 1500)
    })
  },

  handleChoose: function (e) {
    var params = e.currentTarget.dataset
    if (!this.data.supplyItem.id && params.name == 'typeName') {
      wx.showToast({
        icon: 'none',
        title: '请选先选择供应商',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/publish/template/commonlist?apiurl=' + params.apiurl + '&name=' + params.name + '&itemName=' + params.itemname + '&addUrl=' + params.addurl + '&param=' + params.param
    })
  },

  goback: function () {
    wx.navigateBack({})
  },

  //获取内容
  getDetail: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/gds/protected/comm/beanCrud/gds.BrandVO/getById/" + this.data.id,
      method: 'get',
    }, (res) => {
      this.setData({
        cententDetail: res.vo,
        devTypeItem: {
          id: res.vo.devTypeId,
          typeName: res.vo.devTypeName || '请选择设备类型'
        },
        supplyItem: {
          supName: res.vo.supName,
          id: res.vo.supId
        },
      })

    })
  }



})